using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using DAL;
using BLL;
using DTO;

namespace WellmeadowsHospital
{
    public partial class frmSearchStaff : DevExpress.XtraEditors.XtraUserControl
    {
        StaffBLL staffBLL = new StaffBLL();
        QualificationBLL qualificationBLL = new QualificationBLL();
        ExperienceBLL experienceBLL = new ExperienceBLL();
        public frmSearchStaff()
        {
            InitializeComponent();
            BindingData();
        }
        //reset cac text edit
        public void resetSearchStaff()
        {
            txtSearchTypeT.Text = "";
            txtSearchExperienceT.Text = "";
            txtSearchIDNameT.Text = "";
        }
        private void rbtnIDName_Click(object sender, EventArgs e)
        {
            txtSearchTypeT.Enabled = false;
            txtSearchExperienceT.Enabled = false;
            txtSearchIDNameT.Enabled = true;
            resetSearchStaff();
        }

        private void rbtnType_Click(object sender, EventArgs e)
        {
            txtSearchIDNameT.Enabled = false;
            txtSearchExperienceT.Enabled = false;
            txtSearchTypeT.Enabled = true;
            resetSearchStaff();
        }

        private void rbtnExperience_Click(object sender, EventArgs e)
        {
            txtSearchTypeT.Enabled = false;
            txtSearchIDNameT.Enabled = false;
            txtSearchExperienceT.Enabled = true;
            resetSearchStaff();
        }
        public void BindingData()
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = staffBLL.SelectAll();
            navStaff.BindingSource = bs;
            gcResultSearchStaff.DataSource = bs;
            //hien thi StaffID cau dong dau tien trong gridview 
            if (gvSearchStaffT.GetRowCellValue(gvSearchStaffT.SelectedRowsCount - 1, gcStaffIDSearchStaff) != null)
            {
                string staffid = gvSearchStaffT.GetRowCellValue(gvSearchStaffT.SelectedRowsCount - 1, gcStaffIDSearchStaff).ToString();
                BindingSource bs1 = new BindingSource();
                bs1.DataSource = qualificationBLL.Select(staffid);
                navQua.BindingSource = bs1;
                gcSearchQualification.DataSource = bs1;
                BindingSource bs2 = new BindingSource();
                bs2.DataSource = experienceBLL.Select(staffid);
                navExp.BindingSource = bs2;
                gcSearchExperience.DataSource = bs2;
            }
            

        }
        public void BindingDataQuaExp()
        {
            //hien thi StaffID cau dong dau tien trong gridview 
            if (gvSearchStaffT.GetRowCellValue(gvSearchStaffT.SelectedRowsCount - 1, gcStaffIDSearchStaff) != null)
            {
                string staffid = gvSearchStaffT.GetRowCellValue(gvSearchStaffT.SelectedRowsCount - 1, gcStaffIDSearchStaff).ToString();
                BindingSource bs1 = new BindingSource();
                bs1.DataSource = qualificationBLL.Select(staffid);
                navQua.BindingSource = bs1;
                gcSearchQualification.DataSource = bs1;
                BindingSource bs2 = new BindingSource();
                bs2.DataSource = experienceBLL.Select(staffid);
                navExp.BindingSource = bs2;
                gcSearchExperience.DataSource = bs2;

            }

        }

        private void txtSearchIDNameT_KeyPress(object sender, KeyPressEventArgs e)
        {
            peImageSearchT.EditValue = "";
            if (rbtnIDName.Checked)
            {
                BindingSource bs = new BindingSource();
                bs.DataSource=staffBLL.SearchIDName(txtSearchIDNameT.Text);
                navStaff.BindingSource = bs;
                gcResultSearchStaff.DataSource = bs;
                if (gvSearchStaffT.GetRowCellValue(gvSearchStaffT.SelectedRowsCount - 1, gcStaffIDSearchStaff) != null)
                {
                    BindingDataQuaExp();
                }
                //Load anh vao pictureedit
                if (gvSearchStaffT.GetRowCellValue(gvSearchStaffT.SelectedRowsCount - 1, gcImageProfileSearchStaff) != null)
                {
                    // Binary data from MS SQL is received in Base64 format. So you can decode it using the FromBase64String function
                    byte[] a = Convert.FromBase64String(gvSearchStaffT.GetRowCellValue(gvSearchStaffT.SelectedRowsCount - 1, gcImageProfileSearchStaff).ToString().Replace("\"", ""));
                    MemoryStream ms = new MemoryStream(a);
                    Image image = Image.FromStream(ms);
                    peImageSearchT.EditValue = image;
                    //can bang hinh anh trong pictureEdit 
                    peImageSearchT.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Stretch;
                }
            }

        }

        private void txtSearchTypeT_KeyPress(object sender, KeyPressEventArgs e)
        {
            peImageSearchT.EditValue = "";
            if (rbtnType.Checked)
            {
                BindingSource bs = new BindingSource();
                bs.DataSource = staffBLL.SearchType(txtSearchTypeT.Text);
                navStaff.BindingSource = bs;
                gcResultSearchStaff.DataSource = bs;
                BindingDataQuaExp();
                if (gvSearchStaffT.GetRowCellValue(gvSearchStaffT.SelectedRowsCount - 1, gcImageProfileSearchStaff) != null)
                {

                    // Binary data from MS SQL is received in Base64 format. So you can decode it using the FromBase64String function
                    byte[] a = Convert.FromBase64String(gvSearchStaffT.GetRowCellValue(gvSearchStaffT.SelectedRowsCount - 1, gcImageProfileSearchStaff).ToString().Replace("\"", ""));
                    MemoryStream ms = new MemoryStream(a);
                    Image image = Image.FromStream(ms);
                    peImageSearchT.EditValue = image;
                    //can bang hinh anh trong pictureEdit 
                    peImageSearchT.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Stretch;
                }
            }
            if (txtSearchTypeT.Text.Equals(""))
            {
                BindingData();
            }
        }

        private void txtSearchExperienceT_KeyPress(object sender, KeyPressEventArgs e)
        {
            peImageSearchT.EditValue = "";
            if (rbtnExperience.Checked)
            {
                BindingSource bs = new BindingSource();
                bs.DataSource = staffBLL.SearchExperience(txtSearchExperienceT.Text);
                navStaff.BindingSource = bs;
                gcResultSearchStaff.DataSource = bs;
                BindingDataQuaExp();
                //Load anh vao pictureedit

                if (gvSearchStaffT.GetRowCellValue(gvSearchStaffT.SelectedRowsCount - 1, gcImageProfileSearchStaff) != null)
                {

                    // Binary data from MS SQL is received in Base64 format. So you can decode it using the FromBase64String function
                    byte[] a = Convert.FromBase64String(gvSearchStaffT.GetRowCellValue(gvSearchStaffT.SelectedRowsCount - 1, gcImageProfileSearchStaff).ToString().Replace("\"", ""));
                    MemoryStream ms = new MemoryStream(a);
                    Image image = Image.FromStream(ms);
                    peImageSearchT.EditValue = image;
                    //can bang hinh anh trong pictureEdit 
                    peImageSearchT.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Stretch;
                }
            }
            if (txtSearchExperienceT.Text.Equals(""))
            {
                BindingData();
            }
        }

        private void gvSearchStaffT_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            //Load anh vao pictureedit

            if (gvSearchStaffT.GetRowCellValue(gvSearchStaffT.GetSelectedRows()[0], gcImageProfileSearchStaff) != null)
            {

                // Binary data from MS SQL is received in Base64 format. So you can decode it using the FromBase64String function
                byte[] a = Convert.FromBase64String(gvSearchStaffT.GetRowCellValue(gvSearchStaffT.GetSelectedRows()[0], gcImageProfileSearchStaff).ToString().Replace("\"", ""));
                MemoryStream ms = new MemoryStream(a);
                Image image = Image.FromStream(ms);
                peImageSearchT.EditValue = image;
                //can bang hinh anh trong pictureEdit 
                peImageSearchT.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Stretch;
            }
            else
            {
                peImageSearchT.EditValue = "";
            }
        }

        private void gvSearchStaffT_RowClick_1(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            string staffid = gvSearchStaffT.GetRowCellValue(gvSearchStaffT.GetSelectedRows()[0], gcStaffIDSearchStaff).ToString();
            BindingSource bs1 = new BindingSource();
            bs1.DataSource = qualificationBLL.Select(staffid);
            navQua.BindingSource = bs1;
            gcSearchQualification.DataSource = bs1;
            BindingSource bs2 = new BindingSource();
            bs2.DataSource = experienceBLL.Select(staffid);
            navExp.BindingSource = bs2;
            gcSearchExperience.DataSource = bs2;
            if (gvSearchStaffT.GetRowCellValue(gvSearchStaffT.GetSelectedRows()[0], gcImageProfileSearchStaff) != null)
            {

                // Binary data from MS SQL is received in Base64 format. So you can decode it using the FromBase64String function
                byte[] a = Convert.FromBase64String(gvSearchStaffT.GetRowCellValue(gvSearchStaffT.GetSelectedRows()[0], gcImageProfileSearchStaff).ToString().Replace("\"", ""));
                MemoryStream ms = new MemoryStream(a);
                Image image = Image.FromStream(ms);
                peImageSearchT.EditValue = image;
                //can bang hinh anh trong pictureEdit 
                peImageSearchT.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Stretch;
            }
            else
            {
                peImageSearchT.EditValue = "";
            }
            
        }

        private void txtSearchTypeT_EditValueChanged(object sender, EventArgs e)
        {
            if (txtSearchTypeT.Text.Equals(""))
            {
                BindingData();
            }
        }

        private void txtSearchExperienceT_EditValueChanged(object sender, EventArgs e)
        {
            if (txtSearchExperienceT.Text.Equals(""))
            {
                BindingData();
            }
        }


    }
}
